package com.masterclient.app.businessmodule.minemodule.point;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.fragment.FCBaseFragment;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.common.config.BusinessConfig;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.point.adapter.PointExchangeAdapter;
import com.masterclient.app.businessmodule.minemodule.point.contract.PointExchangeContract;
import com.masterclient.app.businessmodule.minemodule.point.contract.PointExchangeContractPresenter;
import com.masterclient.app.businessmodule.minemodule.point.model.PointExchange;
import com.masterclient.app.businessmodule.minemodule.point.model.PointExchangeBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/10/12 15:16
 * 功能说明：
 */

public class PointExchangeFragment extends FCBaseFragment<PointExchangeContractPresenter> implements PointExchangeContract.View{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    PointExchangeAdapter adapter;

    private List<PointExchange> list;
    private int total;
    View emptyView;

    boolean hasMore;

    private int page = 1;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.fragment_point;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new PointExchangeAdapter();
        recyclerView.setAdapter(adapter);
        getData();

        adapter.bindToRecyclerView(recyclerView);
        emptyView = View.inflate(getContext(),R.layout.empty_cash,null);
        emptyView.setVisibility(View.GONE);
        TextView tvTip = (TextView) emptyView.findViewById(R.id.tv_tip);
        tvTip.setText("您还没有兑换记录~");
        TextView tvDesc = (TextView) emptyView.findViewById(R.id.tv_desc);
        tvDesc.setText("未来的你会感谢现在正在奋斗的自己");
        adapter.setEmptyView(emptyView);

        swipeLayout.setOnRefreshListener(()->{
            if(list != null){
                list.clear();
                adapter.notifyDataSetChanged();
            }
            emptyView.setVisibility(View.GONE);
            page = 1;
            getData();
        });

        adapter.setOnLoadMoreListener(()->{
            if(hasMore){
                getData();
            }
        },recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //startActivitryWithBundle(GainInfoActivity.class,new String[][]{{"id",list.get(position).getSh_id()},{"type","1"}});
            }
        });
    }


    public void getData() {
        mPresenter.getPointExchangeData("" + page, BusinessConfig.PAGE_SIZE);
    }

    public void refreshList(){
        if(list != null){
            list.clear();
            adapter.notifyDataSetChanged();
        }
        getData();
    }


    @Override
    public void showData(PointExchangeBean bean) {

        if(swipeLayout!=null&&swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }

        hasMore = bean.getData().isSh_has_more();

        if(list == null){
            list = new ArrayList<>();
        }

        list.addAll(bean.getData().getSh_list());
        adapter.setNewData(list);
        if(list.size() <= 0){
            emptyView.setVisibility(View.VISIBLE);
        }
        if(!hasMore){
            adapter.setEnableLoadMore(false);
        } else {
            page ++;
        }
    }

    @Override
    public void loadFail(String s) {
        if(swipeLayout!=null&&swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        adapter.loadMoreFail();
        ToastUtil.showToast(s);
//        emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void loadError(String s) {
        if(swipeLayout!=null&&swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        ToastUtil.showToast(s);
//        emptyView.setVisibility(View.VISIBLE);
    }

}
